package com.jiuling.apis.controller;

import com.google.common.collect.Lists;
import com.jiuling.apis.model.Pmodel.Porder;
import com.jiuling.apis.service.basic.OrderServiceI;
import com.jswzc.framework.controller.BaseController;
import com.jswzc.framework.util.ObjectExcelView;
import com.jswzc.framework.util.PageData;
import com.jswzc.framework.util.StringUtil;
import com.jswzc.model.Pager;
import com.jswzc.model.Paging;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by tomchen on 17/6/8.
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController{

    @Autowired
    private OrderServiceI orderService;


    @RequestMapping("toOrder")
    public ModelAndView toOrder(HttpServletRequest request, ModelAndView mv) throws Exception {

        mv.setViewName("order/orderList");
        return mv;
    }


    @RequestMapping("queryOrder")
    public void queryOrder(HttpServletResponse response, HttpServletRequest request)throws Exception{

        String identityCode = (String) getSession().getAttribute("identityCode");
        short identity = (short) getSession().getAttribute("identity");


        //必要的分页参数
        Map<String,Object> params=new HashMap<String, Object>();
        //params.put("name", request.getParameter("name")==null?"":request.getParameter("name"));
        //params.put("state", request.getParameter("state"));
        params.put("identityCode", identityCode);
        params.put("identity", identity);
        params.put("name", request.getParameter("name")==null?"":request.getParameter("name"));
        params.put("type", request.getParameter("type"));
        params.put("startDate", request.getParameter("startDate"));
        params.put("endDate", request.getParameter("endDate"));
        Pager<Porder> pager = new Pager();
        pager.setParameters(params);
        pager.setStartRecord(Integer.parseInt(request.getParameter("offset")));
        pager.setPageSize(Integer.parseInt(request.getParameter("limit")));

        pager=orderService.queryOrder(pager);

        Paging<Porder> paging = new Paging<>();
        paging.setRows(pager.getExhibitDatas());
        paging.setTotal((long) pager.getRecordCount());

        writeJson(paging,response);
    }

    @RequestMapping("queryOrderStatic")
    public void queryOrderStatic(HttpServletResponse response, HttpServletRequest request)throws Exception{

        String identityCode = (String) getSession().getAttribute("identityCode");
        short identity = (short) getSession().getAttribute("identity");


        Map<String,Object> params=new HashMap<String, Object>();
        params.put("identityCode", identityCode);
        params.put("identity", identity);
        params.put("name", request.getParameter("name")==null?"":request.getParameter("name"));
        params.put("type", request.getParameter("type"));
        params.put("startDate", request.getParameter("startDate"));
        params.put("endDate", request.getParameter("endDate"));
        Map<String, Object> resultMap = orderService.queryOrderStatic(params);

        writeJson(resultMap,response);
    }

    @RequestMapping(value = "doExcel")
    public ModelAndView doExcel(String name,String type,String startDate,String endDate) throws Exception {

        String identityCode = (String) getSession().getAttribute("identityCode");
        short identity = (short) getSession().getAttribute("identity");

        Map<String,Object> params=new HashMap<String, Object>();
        params.put("identityCode", identityCode);
        params.put("identity", identity);
        params.put("name", URLDecoder.decode(name,"UTF-8"));
        params.put("type", type);// 0发劵 1用劵
        params.put("startDate", startDate);
        params.put("endDate", endDate);


        List<Map<String,Object>> tempList = orderService.queryOrderForExcel(params);


        Map<String,Object> dataMap = new HashMap<>();
        List<String> titles = Lists.newArrayList("商户名称","订单编号","订单金额","发券金额","发贝金额","服务费率","服务费","类型","交易时间");
        List<PageData> varList = new ArrayList<>();
        for (Map<String, Object> map : tempList) {
            PageData vpd = new PageData();
            vpd.put("var1", String.valueOf(map.get("var1")));
            vpd.put("var2", String.valueOf(map.get("var2")));
            vpd.put("var3", String.valueOf(map.get("var3")));
            vpd.put("var4", String.valueOf(map.get("var4")));
            vpd.put("var5", String.valueOf(map.get("var5")));
            vpd.put("var6", String.valueOf(map.get("var6")));
            vpd.put("var7", String.valueOf(map.get("var7")));
            vpd.put("var8", String.valueOf(map.get("var8")));
            vpd.put("var9", String.valueOf(map.get("var9")));
            varList.add(vpd);
        }

        dataMap.put("titles", titles);
        dataMap.put("varList", varList);

        ObjectExcelView erv = new ObjectExcelView(); // 执行excel操作
        mv = new ModelAndView(erv, dataMap);
        return mv;

    }



}
